package org.pzy.ratelimit.controller;

import org.pzy.ratelimit.aspect.Limit;
import org.pzy.ratelimit.aspect.LimitType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author pzy
 */
@RestController
public class LimitController {

    private static final AtomicInteger ATOMIC_INTEGER_1 = new AtomicInteger();
    private static final AtomicInteger ATOMIC_INTEGER_2 = new AtomicInteger();
    private static final AtomicInteger ATOMIC_INTEGER_3 = new AtomicInteger();


    @GetMapping("limit")
    @Limit(key="limit", period = 10, count = 3)
    public Integer limit1() {
        return ATOMIC_INTEGER_1.incrementAndGet();
    }

    @Limit(key="customer_limit", period = 10, count = 3, limitType = LimitType.CUSTOM)
    @GetMapping("customer_limit")
    public Integer limit2() {
        return ATOMIC_INTEGER_2.incrementAndGet();
    }

    @GetMapping("ip_limit")
    @Limit(key="ip_limit", period = 10, count = 3, limitType = LimitType.CUSTOM)
    public Integer limit3() {
        return ATOMIC_INTEGER_3.incrementAndGet();
    }
}
